<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员验证</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card style="height: 200px">
      <h1 style="display: block; text-align: center">身份验证</h1>
      <div style="display: flex; justify-content: center">
        <div>
          <el-input
            v-model="password"
            placeholder="请输入管理员密码"
            style="width: 400px"
          ></el-input>
          <el-button
            type="success"
            icon="el-icon-check"
            circle
            style="margin-left: 20px"
            v-if="this.password === '666'"
          ></el-button>
        </div>
      </div>
    </el-card>

    <el-card v-if="this.password === '666'">
      <el-table :data="userList" border stripe ref="myTable">
        <el-table-column label="*" type="index"></el-table-column>
        <el-table-column label="用户id" prop="id"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="密码" >*********</el-table-column>
        <el-table-column label="邮箱" prop="email"> </el-table-column>

        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showUpdateDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 修改的对话框 -->
    <el-dialog title="修改" :visible.sync="updateDialogVisible" width="30%">
      <!-- 对话框主体区域 -->
      <el-form :model="updateForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="updateForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="updateForm.password"></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input v-model="updateForm.email"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitUpdateForm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      password: "",
      userList: [],
      updateDialogVisible: false,
      updateForm: {},
    };
  },
  created() {
    this.getUserList();
  },
  mounted(){
    
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get("/userList");
      if (!res) return this.$message.error("获取失败");
      this.userList = res;
      this.$message.success("获取用户数据成功");
    },
    async showUpdateDialog(id) {
      const { data: res } = await this.$http.get(`userId/${id}`);
      this.updateForm = res;
      this.updateDialogVisible = true;
    },
    async submitUpdateForm() {
      const { data: res } = await this.$http.put(`userUpdate`, this.updateForm);
      if (!res) return this.$message.error("更新失败");
      this.updateDialogVisible = false;
      this.$message.success("更新用户数据成功");
      this.getUserList();
    },
    async removeUserById(id) {
      const confirmResult = await this.$confirm(
        "请问是否要永久删除该用户",
        "删除提示",
        {
          confirmButtonText: "确认删除",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult != "confirm") {
        return this.$message.info("已经取消删除");
      }
      const { data: res } = await this.$http.delete("user/" + id);
      if (res === 0) return this.$message.error("删除用户失败");
      this.$message.success("删除用户成功");
      this.getUserList();
    },
  },
};
</script>

<style>
</style>
